package com.feizi.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.feizi.entity.OrderdetailEntity;
import com.feizi.entity.OrderinfoEntity;
import com.feizi.entity.Product;
import com.feizi.entity.ShopcartEntity;
import com.feizi.entity.UserEntity;
import com.feizi.service.ProductService;
import com.feizi.service.ShopcartService;
@Controller
public class ShopcartController {
	
	@Autowired
	private ShopcartService shopcartService;
	@Autowired
	private ProductService productService;
	/**
	 * 查询
	 * @param uid
	 * @param model
	 * @return
	 */
	@RequestMapping(path="/findShopcart.do",method=RequestMethod.GET)
	public String findShopcart(HttpSession session,Model model) {
		
		UserEntity user = (UserEntity) session.getAttribute("user");
		
//		if(user == null || "".equals(user)){
//			return "login";
//		}
		
		List<ShopcartEntity> list=shopcartService.findShopcart(user.getUserid());
		model.addAttribute("shopcart", list);
		return "shopcart";
	}
	/**
	 * 修改
	 * @param uid
	 * @param model
	 * @return
	 */
	@RequestMapping(path="/upShopcart.do",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> upShopcart(ShopcartEntity se) {
		Map<String, Object> relMap=new HashMap<String, Object>();
		int rows=shopcartService.upShopcart(se);
		relMap.put("code",rows>0? "0":"1");
		relMap.put("msg",rows>0? "修改成功":"修改失败");
		return relMap;
	}
	/**
	 * 新增(加入购物车)
	 * @param uid
	 * @param model
	 * @return
	 */
	@RequestMapping(path="/addShopcart.do",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addShopcart(HttpSession session,int count,int id,String size) {
		
		UserEntity user = (UserEntity) session.getAttribute("user");
		Map<String, Object> rslMap=new HashMap<String, Object>();
		Product pro=productService.getProduct(id);
		
//		if(user == null || "".equals(user)){
//			rslMap.put("code","0");
//			rslMap.put("msg","请登录" );
//			return rslMap;
//		}
		
		ShopcartEntity shopcart=new ShopcartEntity();
		shopcart.setF_uid(user.getUserid());
		shopcart.setF_pid(id);
		shopcart.setSize(size);
		shopcart.setCount(count);
		shopcart.setPname(pro.getName());
		shopcart.setPimg(pro.getImg());
		shopcart.setPrice(pro.getPrice());
		shopcart.setTotalprice(pro.getPrice()*count);
		int rows=shopcartService.addShopcart(shopcart);
		rslMap.put("code",rows>0?"1":"0");
		rslMap.put("msg",rows>0? "加入购物车成功" : "加入购物车失败" );
		return rslMap;
	}
	/**
	 * 删除
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(path="/delShopcart.do",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> delShopcart(ShopcartEntity se) {
		Map<String, Object> relMap=new HashMap<String, Object>();
		int rows=shopcartService.delShopcart(se.getId());
		relMap.put("code",rows>0? "0":"1");
		relMap.put("msg",rows>0? "删除成功":"删除失败");
		return relMap;
	}
}
